import format from '@dfeidao/atom-web/dt/format';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import cls from '@dfeidao/atom-web/ui/set-node-cls';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';
import whoami from '../../atom/whoami';
import P01 from './P01';
import P02 from './P02';
import P03 from './P03';
export default async function a001(fd: IFeidaoAiBrowserComponent) {
	// todo
	const admin = await whoami(fd) as { params: { productid: string; } };
	const productid = admin.params.productid;
	let page_no = Number(get(fd, 'page_no'));
	let page_size = Number(get(fd, 'page_size'));
	if (!page_no) {
		page_no = 1;
	}
	if (!page_size) {
		page_size = 20;
	}
	const date = new Date().getTime();
	const start_time = date - 3600000;
	const filter = {} as { operation_time: {}, op_cont: string; resume_name: string; productid: string; };
	filter.operation_time = { $gt: start_time, $lt: date };
	filter.productid = productid;
	const f = get(fd, 'f') as { operation_time: number, op_cont: string, resume_name: string; };
	if (f) {
		if (f.operation_time) {
			filter.operation_time = f.operation_time;
		}
		if (f.op_cont) {
			filter.op_cont = f.op_cont;
		}
		if (f.resume_name) {
			filter.resume_name = f.resume_name;
		}
	}
	fd.data.node.querySelector<HTMLInputElement>('[start_time]').value = format(start_time, 0);
	fd.data.node.querySelector<HTMLInputElement>('[end_time]').value = format(date, 0);
	if (get(fd, 'start_time')) {
		fd.data.node.querySelector<HTMLInputElement>('[start_time]').value = get(fd, 'start_time');
	}
	if (get(fd, 'end_time')) {
		fd.data.node.querySelector<HTMLInputElement>('[end_time]').value = get(fd, 'end_time');
	}
	show_loading();
	const res = await nodejs<{ data: Array<{}>; count: number; page_no: number; page_size: number; }>('operation-log/zj-000001/s001', { filter, page_no, page_size });
	res.page_no = page_no;
	res.page_size = page_size;
	const P1 = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P01"]');
	const P2 = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P02"]');
	const P3 = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P03"]');
	if (res.count) {
		if (res.count > 20) {
			cls(P1, 'display-n', false);
			cls(P2, 'display-n', false);
			cls(P3, 'display-n', true);
			render(fd, res.data, P01, 'P01', 'inner');
			render(fd, res, P02, 'P02', 'inner');
		} else {
			cls(P1, 'display-n', false);
			cls(P2, 'display-n', true);
			cls(P3, 'display-n', true);
			render(fd, res.data, P01, 'P01', 'inner');
		}
	} else {
		cls(P1, 'display-n', true);
		cls(P2, 'display-n', true);
		cls(P3, 'display-n', false);
		render(fd, '', P03, 'P03', 'inner');
	}
	hide_loading();
}
